﻿// -----------------------------------------------------------------------
// <copyright file="WorkData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Work
    /// </summary>
    public class WorkData
    {
        /// <summary>
        /// Get Work By Id
        /// </summary>
        /// <param name="workId">Param workId</param>
        /// <returns>Return Work</returns>
        public WorkEN GetWorkById(int workId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkEN work = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkGetById, workId);
                while (reader.Read())
                {
                    work = new WorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        work.WorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        work.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        work.NodeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        work.UserId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        work.Date = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        work.Content = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        work.DepartmentId = reader.GetInt32(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return work;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Work
        /// </summary>
        /// <returns>Return list all Work</returns>
        public IList<WorkEN> GetWorks()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<WorkEN> lstWork = new List<WorkEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkGetAll);
                while (reader.Read())
                {
                    WorkEN work = new WorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        work.WorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        work.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        work.NodeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        work.UserId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        work.Date = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        work.Content = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        work.DepartmentId = reader.GetInt32(6);
                    }

                    lstWork.Add(work);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstWork;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Work
        /// </summary>
        /// <param name="work">Paarm Work</param>
        /// <returns>Insert Work</returns>
        public int Insert(WorkEN work)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Work.ProfileId, work.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.NodeId, work.NodeId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.UserId, work.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.Date, (object)work.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Work.Content, work.Content),
                                    new SqlParameter(Constants.SqlParamerterName.Work.DepartmentId, work.DepartmentId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Work
        /// </summary>
        /// <param name="work">Param Work</param>
        public void Update(WorkEN work)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Work.WorkId, work.WorkId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.ProfileId, work.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.NodeId, work.NodeId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.UserId, work.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.Work.Date, (object)work.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Work.Content, work.Content),
                                    new SqlParameter(Constants.SqlParamerterName.Work.DepartmentId, work.DepartmentId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Work by workId
        /// </summary>
        /// <param name="workId">param workId</param>
        public void Delete(int workId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Work.WorkId, workId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List Work By profileId
        /// </summary>
        /// <param name="profileId">Param profileId</param>
        /// <returns>Return List Work</returns>
        public IList<WorkEN> GetWorkByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<WorkEN> lstWork = new List<WorkEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkGetByProfileId, profileId);
                while (reader.Read())
                {
                    WorkEN work = new WorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        work.WorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        work.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        work.NodeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        work.UserId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        work.Date = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        work.Content = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        work.DepartmentId = reader.GetInt32(6);
                    }

                    lstWork.Add(work);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstWork;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the work by user work.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="workId">The work id.</param>
        /// <returns>
        /// return WorkEN
        /// </returns>
        public WorkEN GetWorkByUser_Work(int userId, int workId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkEN work = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkGetByUserWork, userId, workId);
                while (reader.Read())
                {
                    work = new WorkEN();

                    if (!reader.IsDBNull(0))
                    {
                        work.WorkId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        work.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        work.NodeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        work.UserId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        work.Date = reader.GetDateTime(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        work.Content = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        work.DepartmentId = reader.GetInt32(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return work;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}